import Login from '@/views/login'
import { Navigate, useRoutes } from 'react-router-dom'
import lazyLoad from '@/router/utils/lazyLoad'
import React from 'react'
import { store } from '@/store/index'
import { Route } from '@/model/route'
// * 导入所有router
const metaRouters = import.meta.globEager('./modules/*.tsx')
// * 处理路由
export const routerSortArray: Route[] = []
Object.keys(metaRouters).forEach((item) => {
  Object.keys(metaRouters[item]).forEach((key: any) => {
    routerSortArray.push(...metaRouters[item][key])
  })
})

const token = store.getState().user.token
const storeRoute: Route[] =
  store.getState().route.routeList || []

/**
 * 基本路由配置
 * @param {string} path 路径
 * @param {string} element 组件的页面
 * @param {string} matcher 需要匹配的路径
 * @param {string} name 菜单名
 * @param {Route} children 子页面
 * @param {boolean} sidebar 菜单栏
 */

export let rootRouter: Route[] = [
  {
    path: '/login',
    element: <Login />,
    meta: {
      title: '登录页'
    }
  },
  {
    path: '/',
    element: <Navigate to="/login" />
  }
]
if (token) {
  rootRouter = [
    {
      path: '/login',
      element: <Login />,
      meta: {
        title: '登录页'
      }
    },
    {
      path: '/',
      element: <Navigate to="/login" />
    }
  ]
  if (storeRoute.length) {
    rootRouter.push(...storeRoute)
  } else {
    rootRouter.push(...routerSortArray)
  }
}

/**
 * 错误路由配置
 * @param {string} path 路径
 * @param {string} element 组件的页面
 * @param {string} name 菜单名
 * @param {Route} children 子页面
 * @param {boolean} sidebar 菜单栏
 */
export const errorRouter: Route[] = [
  {
    path: '/404',
    element: lazyLoad(
      React.lazy(() => import('@/views/404/index'))
    ),
    meta: {
      title: '404页面'
    }
  },
  {
    path: '*',
    element: <Navigate to="/404" />
  }
]

const Router = () => {
  const routes = useRoutes(rootRouter)
  return routes
}

export default Router
